package com.mcmaster.android.menus;

import java.io.FileNotFoundException;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.mcmaster.android.Output;
import com.mcmaster.android.R;
import com.mcmaster.android.R.layout;
import com.mcmaster.android.control.ScavengerMenuControl;
import com.mcmaster.android.data.Building;
import com.mcmaster.android.data.TourDatabase;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ScavengerMenu extends MapActivity
{
	String contents_;
	Building current_building_;
	MapController mapctrl_;
	ScavengerMenuControl controller_;
	
	public void onCreate(Bundle savedInstanceState)
	{
		Bundle extras = getIntent().getExtras();
		String tour_name_ = extras.getString("name");
		
		super.onCreate(savedInstanceState);
		setContentView(layout.scavenger_layout_1);
		
		MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        
		Button scv_return_btn = (Button)findViewById(R.id.scv_return_btn);
		Button scv_scan_btn = (Button)findViewById(R.id.scv_scan_btn);
		Button scv_hint_btn = (Button)findViewById(R.id.scv_hint_btn);
		
        scv_return_btn.setOnClickListener(new ButtonHandlerHomeMenu());
        scv_scan_btn.setOnClickListener(new ButtonHandlerScan());
        scv_hint_btn.setOnClickListener(new ButtonHandlerHint());
        
    	try {
			controller_ = new ScavengerMenuControl(this, mapView, TourDatabase.getTourFromName(tour_name_));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private class ButtonHandlerHomeMenu implements View.OnClickListener {
	    public void onClick(View v) {
	    	handleHomeClick();
	    }
	}
	
	private class ButtonHandlerScan implements View.OnClickListener {
	    public void onClick(View v) {
	    	try {
				handleScanClick();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	}
	
	private class ButtonHandlerHint implements View.OnClickListener {
	    public void onClick(View v) {
	    	handleHintClick();
	    }
	}
	
	private void handleHomeClick()
	{
		finish();
	}
	
	private void handleHintClick()
	{
		controller_.showHint();
	}
	
	private void handleScanClick() throws FileNotFoundException
	{
		IntentIntegrator integrator = new IntentIntegrator(ScavengerMenu.this);
		integrator.initiateScan();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (result != null) { //if something has actually happened
        String contents = result.getContents(); //fetch the contents
        if (contents != null) {
        	//If a QR code is scanned, the following code is executed
        	//The QR code itself is stored in a string named "contents", it's what should be passed through your functions
        	//Toast.makeText(GuidedMenu.this, contents, Toast.LENGTH_SHORT).show(); //debug line, displays what was contained in the code
        	controller_.startQRReader(contents);
        	//NOTE: Technically, you can scan things other than QR codes with the barcode scanner. If this is a problem, let Nathan know
        } else {
        	//If no QR code was detected, then the code here will be executed
        }
      }
    }
}

